package com.ruoyi.tuanzhang.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ruoyi.tuanzhang.domain.excelDto.*;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 处理成excel表
 * */
@Component
public class ExcelUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月DD日");

    //    @Value("${basePath}")
//    private static String basePath;

    /**
     * 多个sheet表
     * */
    public static void getMulSheetExcel(DarenDataListDto darenDataListDto, String basePath){
        String fileName = basePath + sdf.format(new Date()) + System.currentTimeMillis() + ".xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {
            WriteSheet baseSheet = EasyExcel.writerSheet(0, "达人基本信息").head(DarenBaseExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenBaseExcelDtoList(), baseSheet);
            WriteSheet homeSheet = EasyExcel.writerSheet(1, "达人详细信息").head(DarenHomeExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenHomeExcelDtoList(), homeSheet);
            WriteSheet homeFansSheet = EasyExcel.writerSheet(2, "达人粉丝信息").head(DarenHomeFansDataExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenHomeFansDataExcelDtoList(), homeFansSheet);
            WriteSheet homeScoreSheet = EasyExcel.writerSheet(3, "达人评分信息").head(DarenHomeScoreDataExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenHomeScoreDataExcelDtoList(), homeScoreSheet);
            WriteSheet homeSellSheet = EasyExcel.writerSheet(4, "达人销售信息").head(DarenHomeSellDataExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenHomeSellDataExcelDtoList(), homeSellSheet);
            WriteSheet homeVideoSheet = EasyExcel.writerSheet(5, "达人视频数据信息").head(DarenHomeVideoDataExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenHomeVideoDataExcelDtoList(), homeVideoSheet);
            WriteSheet homeVideoHistorySheet = EasyExcel.writerSheet(6, "达人视频历史信息").head(DarenHomeVideoHistoryExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenHomeVideoHistoryExcelDtoList(), homeVideoHistorySheet);
        }

    }


    /**
     * 生成基础表
     * */
    public static void getBaseSheetExcel(DarenDataListDto darenDataListDto, String basePath){
        String fileName = basePath + sdf.format(new Date()) + System.currentTimeMillis() + ".xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {
            WriteSheet baseSheet = EasyExcel.writerSheet(0, "达人基本信息").head(DarenBaseSinExcelDto.class).build();
            excelWriter.write(darenDataListDto.getDarenBaseExcelDtoList(), baseSheet);
        }
    }

    /**
     * 生成货盘表
     * */
    public static void getProductSheetExcel(List<ProductListExcelDto> productDataListDto, String basePath){
        String fileName = basePath + "货盘//" + sdf.format(new Date()) + "货盘.xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {
            WriteSheet baseSheet = EasyExcel.writerSheet(0, "货盘表").head(ProductListExcelDto.class).build();
            excelWriter.write(productDataListDto, baseSheet);
        }
    }




//    /**
//     * 测试简单表输出
//     * */
//    public static void testSimpleWrite(){
////        DarenBaseDo darenBaseDo = new DarenBaseDo();
//        List<DarenBaseExcelDto> list = new ArrayList<>();
//        DarenBaseExcelDto darenBaseExcelDto = new DarenBaseExcelDto();
//        darenBaseExcelDto.setAwemeId("11111");
//        darenBaseExcelDto.setCategoryTopDesc("11111");
//        darenBaseExcelDto.setBrandTopDecs("11111");
//        darenBaseExcelDto.setCreateTime(new Date());
//        list.add(darenBaseExcelDto);
//        String fileName = "C:\\Users\\admin\\Desktop\\" + System.currentTimeMillis() + ".xlsx";
////        EasyExcel.write(fileName, DarenBaseExcelDto.class).sheet("模板").doWrite(list);
//
//        try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {
//            WriteSheet writeSheet = EasyExcel.writerSheet(0, "模板" + 0).head(DarenBaseExcelDto.class).build();
//            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
//            excelWriter.write(list, writeSheet);
//
//            // 不同sheet应该分开
//            WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "模板" + 1).head(DarenBaseExcelDto.class).build();
//            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
//            excelWriter.write(list, writeSheet1);
//        }
//    }
//
//    public static void main(String[] args) {
//        testSimpleWrite();
//    }
}
